/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package be.wiim;

import com.pi4j.io.gpio.GpioController;
import com.pi4j.io.gpio.GpioPinDigitalOutput;
import com.pi4j.io.gpio.RaspiPin;

/**
 *
 * @author wimvandecasteele
 */
public class LedController {
    
    static LedController fLedController;
    private GpioPinDigitalOutput vGreenLed;
    private GpioPinDigitalOutput vRedLed;
    private GpioPinDigitalOutput vOrangeLed;
    
    
    public LedController(GpioController pGpioController) 
    {
        fLedController = this;
        vGreenLed = pGpioController.provisionDigitalOutputPin(RaspiPin.GPIO_01);
        vRedLed = pGpioController.provisionDigitalOutputPin(RaspiPin.GPIO_02);
        vOrangeLed = pGpioController.provisionDigitalOutputPin(RaspiPin.GPIO_03);
    }
    
    static LedController getInstance()
    {
        return fLedController;
    }
    
    public void setLedsForHudsonStatus(String pStatus)
    {
        setStatusLedsLow();
        if(pStatus.indexOf(HudsonStatus.green.toString()) > -1)
        {
            vGreenLed.high();
        }
        if(pStatus.indexOf(HudsonStatus.red.toString()) > -1)
        {
            vRedLed.high();
        }
        if(pStatus.indexOf(HudsonStatus.red.toString()) == -1 && 
           pStatus.indexOf(HudsonStatus.green.toString()) == -1)
        {
            vOrangeLed.high();
        }
    }
    
    public void setLedsForTempHudsonStatus(String pStatus)
    {
        if(pStatus.indexOf(HudsonStatus.green.toString()) > -1)
        {
            vGreenLed.toggle();
        }
        if(pStatus.indexOf(HudsonStatus.red.toString()) > -1)
        {
            vRedLed.toggle();
        }
        if(pStatus.indexOf(HudsonStatus.red.toString()) == -1 && 
           pStatus.indexOf(HudsonStatus.green.toString()) == -1)
        {
            vOrangeLed.toggle();
        }
    }
    
    public void setStatusLedsLow()
    {
        vGreenLed.low();
        vRedLed.low();
        vOrangeLed.low();
    }
    
}
